-
Notifications
You must be signed in to change notification settings - Fork 451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved javascript template string expression extracting #792
base: master
Are you sure you want to change the base?
Improved javascript template string expression extracting #792
Conversation
Hi @akx . I've been working on this feature for a while. In my setup I need these changes. However I have some problems when using my own fork in my project. So I have some questions regarding using a fork of babel in my project, and also what needs to be done to get this merged, so I don't have to use my fork anymore. In my project, which uses Pipenv. Inside the
This works partly. When I run However, when I run my Django devserver, on some requests (not all of them, I'm not sure yet why it only happens on some requests), the request fails with a exception coming from
I noticed that when I go into the virtualenv directory (which is created by Pipenv and can be retrieved with I will have to look into the possibilities to integrate this in my project in a way that the locale files would be generated when I build and deploy my project. In the meantime it would be good for us if the changes would eventually be merged into the official package. So I have two questions:
|
Hi @gitaarik, sorry for the latency. I had somehow inadvertently turned all notifications about Babel off (or maybe had just dropped the ball on this one). Could you rebase this on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beyond the other comments, I'd appreciate some tests :)
|
||
fake_file_obj = io.BytesIO(expression_contents.encode()) | ||
|
||
for item in extract_javascript(fake_file_obj, keywords, comment_tags, options): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this interact with e.g. line numbering?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line numbering has been fixed in #939. There is now also a test for that.
def parse_template_string(template_string, fileobj, keywords, comment_tags, options): | ||
|
||
prev_character = None | ||
level = 0 | ||
inside_str = False | ||
expression_contents = '' | ||
|
||
for character in template_string[1:-1]: | ||
|
||
if not inside_str and character in ('"', "'", '`'): | ||
inside_str = character | ||
elif inside_str == character and prev_character != r'\\': | ||
inside_str = False | ||
|
||
if level: | ||
expression_contents += character | ||
|
||
if not inside_str: | ||
|
||
if character == '{' and prev_character == '$': | ||
level += 1 | ||
|
||
elif level and character == '}': | ||
|
||
level -= 1 | ||
|
||
if level == 0 and expression_contents: | ||
|
||
expression_contents = expression_contents[0:-1] | ||
|
||
fake_file_obj = io.BytesIO(expression_contents.encode()) | ||
|
||
for item in extract_javascript(fake_file_obj, keywords, comment_tags, options): | ||
yield item | ||
|
||
expression_contents = '' | ||
|
||
prev_character = character |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove the empty lines within the blocks, so the spacing of this code (which is quite airy 😄 ) matches the rest of the codebase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #939.
Tests have been added in #939. |
Add option
parse_template_string
that improves parsing of javascript templates expressions, like:And it also works with nested expressions like:
You have to enable template string extraction with an option in the
option_map
:Then it should work.